package com.cloud.springcloudhysyrix.hebingqingqiu;

import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 请求合并，发起请求类
 * weiximei on 2018/6/1
 */
public class WxmCommand extends HystrixCommand<List<String>> {

    private RestTemplate restTemplate;

    /**
     * 请求合并的id集合
     */
    private List<Long> ids;

    /**
     *
     * @param commandGroupKey
     * @param restTemplate
     * @param ids 需要请求合并的id传进来
     */
    protected WxmCommand(String commandGroupKey,RestTemplate restTemplate,List<Long> ids) {
        super(HystrixCommandGroupKey.Factory.asKey(commandGroupKey));
        this.restTemplate = restTemplate;
        this.ids = ids;
    }

    @Override
    protected List<String> run() throws Exception {
        System.out.println("发送请求。。参数为："+ids.toString()+Thread.currentThread().getName());
        // StringUtils.join(ids,",") 表示把 ids集合，合并并用"，"分割开，然后放入{1}位置
        String [] result = restTemplate.getForEntity("http://HELLO-SERVER/weiximei2?ids={1}",String[].class,StringUtils.join(ids,",")).getBody();
        return Arrays.asList(result);
    }

    /**
     * 那么请求合并由谁去控制？
     *    WxmBatchCommand控制
     */

}
